// 格式当前日期
function getCurrentDate(){
   let date = new Date();
   let YY = date.getFullYear();
   let MM = date.getMonth() + 1;
   let DD = date.getDate();
   MM = MM < 10 ? '0'+MM : MM;
   DD = DD < 10 ? '0'+DD : DD;
   return `${YY}/${MM}/${DD}`
}

// 格式换人民币
function formatMoney(value){
  let str = Math.abs(value).toString()
  let txt = '';
  if(str.includes('.')){
    let i = str.lastIndexOf('.')
    let value = str.slice(i+1)
    if(value.length==1){
      txt = str+"0"
    }
    else {
      txt = str+"";
    }
  }
  else {
     txt = str+".00"
  }
  return txt;
}

Page({

  /**
   * 页面的初始数据
   */
  data: { 
    date: getCurrentDate(),
    // 当前月份的余额
    currentMonthTotal: '0.00',
    // 当前月份的存钱
    currentMonthIncome: '0.00',
    // 当前月份的支出
    currentMonthOutcome: '0.00',

    // 当天的余额
    currentDayTotal: '0.00',
    // 当天的存钱
    currentDayIncome: '0.00',
    // 当天的支出
    currentDayOutcome: '0.00',

    // 总额
    total:0,

    // 定义数组获取本地存储的数据
    elements: [],

    // 获取当前的记账列表
    dayList: []
  },
  // 选择日期
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let date = e.detail.value;
    let dateStr = date.toString().replace(/-/g,'/')
    console.log("dateStr:",dateStr)
    this.setData({
      date: dateStr
    })
    // 过滤数据
    this.getCurrentDayList();
    this.getCurrentMonthList();
  },

  // 编写方法获取本地存储的数据
  getElements(){
     let elements = wx.getStorageSync('ELEMENTS_JIZHANG');
    //  console.log("1.0",elements)
     if(elements){
       this.setData({
         elements
       })
     }
  },

  // 编写函数获取当天数据
  getCurrentDayList(){
      let elements = this.data.elements;
      let date = this.data.date;
      let arr = elements.filter(item=>{
          if(item.datetime.includes(date)){
            return item;
          }
      })
      if(arr.length == 0) {
        this.setData({
          currentDayTotal: formatMoney(0),
          currentDayIncome:formatMoney(0),
          currentDayOutcome:formatMoney(0),
          dayList:[]
        })
      }
      else {
        let currentDayIncome = 0;
        let currentDayOutcome = 0;
        for(let i = 0 ; i < arr.length ; i ++){
            if(arr[i].num == '1'){
              currentDayOutcome+=arr[i].money*1000;
            }
            if(arr[i].num == '2'){
              currentDayIncome+=arr[i].money* 1000;
            }
        }
        let currentDayTotal = currentDayIncome - currentDayOutcome
        this.setData({
          currentDayTotal: formatMoney(currentDayTotal/1000),
          currentDayIncome:formatMoney(currentDayIncome/1000),
          currentDayOutcome:formatMoney(currentDayOutcome/1000),
        })

        let newArr = arr.map(item=>{
          item.money = formatMoney(item.money)
          return item;
        })

        this.setData({
          dayList: newArr,
        })
      }
  },

  // 编写函数获取当月数据
  getCurrentMonthList(){
    let elements = this.data.elements;
    let date = this.data.date && this.data.date.slice(0,7);
    console.log("年月:",date)
    let arr = elements.filter(item=>{
        if(item.datetime.includes(date)){
          return item;
        }
    })
    if(arr.length == 0) {
      this.setData({
        currentMonthIncome: formatMoney(0),
        currentMonthOutcome: formatMoney(0),
        currentMonthTotal: formatMoney(0)
      })
    }
    else {
       // 当前月份的存钱
        let  currentMonthIncome = 0
        // 当前月份的支出
        let  currentMonthOutcome = 0
        // 循环当前月份的数组
        for(let i = 0 ; i < arr.length ; i ++){
            if(arr[i].num == '1'){
              currentMonthOutcome+=(arr[i].money) * 1000
            }
            if(arr[i].num == '2'){
              currentMonthIncome+=(arr[i].money) * 1000
            }
        }
        let currentMonthTotal = currentMonthIncome - currentMonthOutcome;
        // console.log("currentMonthTotal--->",currentMonthTotal)
        let vTotal = currentMonthTotal < 0 ?  "-"+ formatMoney(currentMonthTotal / 1000) : formatMoney(currentMonthTotal / 1000);
        // 赋值
        this.setData({
          currentMonthIncome: formatMoney(currentMonthIncome / 1000),
          currentMonthOutcome: formatMoney(currentMonthOutcome / 1000),
          currentMonthTotal: vTotal,
        })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      // 初始化
      this.getElements();
      this.getCurrentDayList();
      this.getCurrentMonthList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 初始化
    this.getElements();
    this.getCurrentDayList();
    this.getCurrentMonthList();
  },

})